package cfgs.gestudent.finestra.alumnes;

import java.awt.Color;
import java.util.ResourceBundle;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import cfgs.gestudent.dao.alumnes.EliminarAlumne;
import cfgs.gestudent.dao.alumnes.MostrarAlumnes;
import cfgs.gestudent.dao.alumnes.TaulaControladoraModificacions;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;
import cfgs.gestudent.preferencies.PreferenciesAnyAcademic;

import java.awt.GridBagLayout;

import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FinestraMostrarAlumnes extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6109229429354750371L;
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	private JTable table;
	private JScrollPane scpTable;
	private JScrollPane scpTable_1;
	private DefaultTableModel dtmEjemplo;
	private MostrarAlumnes ma = null;
	private JTextField textField;
	private JComboBox comboBox;
	private JPopupMenu popupMenu;
	private JMenuItem mntmBorrar;
	
	private String sql;
	
	//variables per modificar el camp

	/**
	 * Create the panel.
	 */
	public FinestraMostrarAlumnes() {
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {0, 30, 30, 30, 30, 30, 0, 0};
		gridBagLayout.rowHeights = new int[] {0, 30, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE, 1.0};
		setLayout(gridBagLayout);
		
		ma = new MostrarAlumnes();
		
		//Dades model combo
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String selecCombo = comboBox.getSelectedItem().toString();
				if(selecCombo != rb.getString("cercarPerCombAl")){
					textField.enable();
				}else{
					textField.disable();
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(new String[] {rb.getString("cercarPerCombAl"), rb.getString("nomCombAl"), rb.getString("cognomCombAl"), rb.getString("cursCombAl"), rb.getString("grupCombAl")}));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 4;
		gbc_comboBox.gridy = 0;
		add(comboBox, gbc_comboBox);
		
		textField = new JTextField();
		textField.getDocument().addDocumentListener(new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				String selecCombo = comboBox.getSelectedItem().toString();
				if(selecCombo != rb.getString("cercarPerCombAl")){
					String sql = "";
					String txtValue = textField.getText();
					if(selecCombo == rb.getString("nomCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND al.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else
					if(selecCombo == rb.getString("cognomCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND al.Cognoms LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else
					if(selecCombo == rb.getString("cursCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND gc.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else 
					if(selecCombo == rb.getString("grupCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND c.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}
					
					if(sql != ""){
						ma.setSql(sql);
						dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
							@Override
						    public boolean isCellEditable (int row, int column)
						    {
								if (column == 0)
							           return true;
							        
							    if(column == 1)
							        	return true;
							    
							    if (column == 2)
						           return false;
						        
						        if(column == 3)
						        	return false;
						        			
						        return false;
						    }
						};
						table.setModel(dtmEjemplo);
						/**
						 * Modificar 
						 */
						table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Nom", 4));
						table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Cognoms", 4));
						table.getColumnModel().getColumn(3).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Id_Grup_Curs", 6));
					}
				}
			
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				String selecCombo = comboBox.getSelectedItem().toString();
				if(selecCombo != rb.getString("cercarPerCombAl")){
					String sql = "";
					String txtValue = textField.getText();
					if(selecCombo == rb.getString("nomCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND al.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else
					if(selecCombo == rb.getString("cognomCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND al.Cognoms LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else
					if(selecCombo == rb.getString("cursCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND gc.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else 
					if(selecCombo == rb.getString("grupCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND c.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}
					
					if(sql != ""){
						ma.setSql(sql);
						dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
							@Override
						    public boolean isCellEditable (int row, int column)
						    {
								if (column == 0)
							           return true;
							        
							    if(column == 1)
							        	return true;
							    
							    if (column == 2)
						           return false;
						        
						        if(column == 3)
						        	return false;
						        			
						        return false;
						    }
						};
						table.setModel(dtmEjemplo);
						/**
						 * Modificar 
						 */
						table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Nom", 4));
						table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Cognoms", 4));
						table.getColumnModel().getColumn(3).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Id_Grup_Curs", 6));
					}
				}
			
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				String selecCombo = comboBox.getSelectedItem().toString();
				if(selecCombo != rb.getString("cercarPerCombAl")){
					String sql = "";
					String txtValue = textField.getText();
					if(selecCombo == rb.getString("nomCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND al.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else
					if(selecCombo == rb.getString("cognomCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND al.Cognoms LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else
					if(selecCombo == rb.getString("cursCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND gc.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}else 
					if(selecCombo == rb.getString("grupCombAl")){
						sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" AND c.Nom LIKE'%"+txtValue+"%'"+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
					}
					
					if(sql != ""){
						ma.setSql(sql);
						dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
							@Override
						    public boolean isCellEditable (int row, int column)
						    {
								if (column == 0)
							           return true;
							        
							    if(column == 1)
							        	return true;
							    
							    if (column == 2)
						           return false;
						        
						        if(column == 3)
						        	return false;
						        			
						        return false;
						    }
						};
						table.setModel(dtmEjemplo);
						/**
						 * Modificar 
						 */
						table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Nom", 4));
						table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Cognoms", 4));
						table.getColumnModel().getColumn(3).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Id_Grup_Curs", 6));
					}
				}
			}
		});
		
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 5;
		gbc_textField.gridy = 0;
		add(textField, gbc_textField);
		textField.setColumns(10);
		textField.disable();
		
		table = new JTable();
		scpTable_1 = new JScrollPane();
		
		scpTable_1.setViewportView(table);
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 7;
		gbc_table.insets = new Insets(0, 0, 5, 0);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 0;
		gbc_table.gridy = 1;
		add(scpTable_1, gbc_table);
		
		sql = "SELECT al.Nom AS Noms, al.Cognoms AS Cognoms, c.Nom AS Curs, gc.Nom AS Grup, al.Id AS Id, c.Id, gc.Id FROM ALUMNE al, GRUP_CURS gc, CURS c, ANY_ACADEMIC aa WHERE al.Id_Grup_Curs=gc.Id AND gc.Id_Curs=c.Id AND aa.Id=c.Id_Any_Academic AND aa.Id="+veureIdAnyAncademic()+" ORDER BY c.Id, gc.Id, Cognoms ASC ";
		
		ma.setSql(sql);
		dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
			@Override
		    public boolean isCellEditable (int row, int column)
		    {
				if (column == 0)
			           return true;
			        
			    if(column == 1)
			        	return true;
			    
			    if (column == 2)
		           return false;
		        
		        if(column == 3)
		        	return false;
		        			
		        return false;
		    }
		};
		table.setModel(dtmEjemplo);
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		
		/**
		 * Modificar 
		 */
		table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Nom", 4));
		table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Cognoms", 4));
		table.getColumnModel().getColumn(3).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Id_Grup_Curs", 6));
		
//		TableRowSorter<TableModel> sort = new TableRowSorter<TableModel>(dtmEjemplo);
//		table.setRowSorter(sort);
		

		popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		mntmBorrar = new JMenuItem(rb.getString("borrar"));
		mntmBorrar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedRow();
				Object [][] obj = ma.getFilas();
				int idBorrar = (int) obj[row][4];
				EliminarAlumne ea = new EliminarAlumne();
				ea.eliminarRegistre(idBorrar, table.getValueAt(row, 0).toString()+" - "+table.getValueAt(row, 1).toString());
				ma.setSql(sql);
				dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
					@Override
				    public boolean isCellEditable (int row, int column)
				    {
						if (column == 0)
					           return true;
					        
					    if(column == 1)
					        	return true;
					    
					    if (column == 2)
				           return false;
				        
				        if(column == 3)
				        	return false;
				        			
				        return false;
				    }
				};
				table.setModel(dtmEjemplo);
				/**
				 * Modificar 
				 */
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Nom", 4));
				table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Cognoms", 4));
				table.getColumnModel().getColumn(3).setCellEditor(new TaulaControladoraModificacions(ma.getFilas(), "Id_Grup_Curs", 6));
			}
		});
		popupMenu.add(mntmBorrar);
		
		

	}
	
	
	private int veureIdAnyAncademic(){
		int idAny = 0;
		PreferenciesAnyAcademic paa = new PreferenciesAnyAcademic();
		idAny = paa.getAnyAcademic();
		
		return idAny;
	}
	

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	public void recarregarTaula(){
		
	}
}
